package com.aigou.admin.controller;

import com.aigou.admin.entity.Product;
import com.aigou.admin.entity.ResponseEntity;
import com.aigou.admin.service.ProductService;
import com.aigou.admin.service.impl.ProductServiceImpl;
import com.alibaba.fastjson2.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.List;

@WebServlet("/product")
public class ProductController extends BaseServlet{

    //调用service
    private ProductService ProductService = new ProductServiceImpl();

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获得商品id
        String ProductId = req.getParameter("productId");
        //查询商品
        boolean rs = ProductService.remove(ProductId);
        //转换为JSON,通过流发送给客户端
        resp.getWriter().write(JSON.toJSONString(new ResponseEntity("OK",rs)));
    }

    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询商品
        List<Product> Products = ProductService.findAll();
        //转换为JSON,通过流发送给客户端
        resp.getWriter().write(JSON.toJSONString(new ResponseEntity("OK",Products)));
    }

    public void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获得商品id
        String ProductId = req.getParameter("productId");
        //查询商品
        Product Product = ProductService.findById(ProductId);
        //转换为JSON,通过流发送给客户端
        resp.getWriter().write(JSON.toJSONString(new ResponseEntity("OK",Product)));
    }

    public void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获得商品id
        String id = req.getParameter("product_id");
        String name = req.getParameter("product_name");
        String productImage = req.getParameter("product_image");
        String price = req.getParameter("price");
        String productType = req.getParameter("product_type");
        String productDesc = req.getParameter("product_desc");
        String productBrand = req.getParameter("product_brand");
        Timestamp createTime = Timestamp.from(Instant.now());

        Product Product = new Product(id,name,productImage,Float.valueOf(price),productType,productDesc,
                createTime,productBrand);
        //查询商品
        boolean rs = ProductService.save(Product);
        //转换为JSON,通过流发送给客户端
        resp.getWriter().write(JSON.toJSONString(new ResponseEntity("OK",rs)));
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获得商品id
        String id = req.getParameter("product_id");
        String name = req.getParameter("product_name");
        String productImage = req.getParameter("product_image");
        String price = req.getParameter("price");
        String productType = req.getParameter("product_type");
        String productDesc = req.getParameter("product_desc");
        String productBrand = req.getParameter("product_brand");
        Timestamp createTime = Timestamp.from(Instant.now());
        Product Product = new Product(id,name,productImage,Float.valueOf(price),productType,productDesc,
                createTime,productBrand);
        //查询商品
        boolean rs = ProductService.update(Product);
        //转换为JSON,通过流发送给客户端
        resp.getWriter().write(JSON.toJSONString(new ResponseEntity("OK",rs)));
    }
}
